/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.ycl.wx.audio.yzserver.utils;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class HttppostUtil {

    static Logger logger = Logger.getLogger(HttppostUtil.class.getName());

    public static InputStream sendJson(String json, String post) {
        InputStream result = null;
        HttpPost httppost = null;
        try {
            //默认字符编码是ISO-8859-1
            HttpClient httpClient = new DefaultHttpClient();

            httppost = new HttpPost(post);

            InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(json.getBytes("UTF-8")), -1);
            reqEntity.setContentType("application/octet-stream");// 区分不同种类的数据,xml数据
            reqEntity.setContentEncoding("UTF-8");// charset=UTF-8
            httppost.setEntity(reqEntity);

            HttpResponse response = httpClient.execute(httppost);
            StatusLine sl = response.getStatusLine();

            if (sl.getStatusCode() == 200) {
                result=response.getEntity().getContent();
            } else {
                result = response.getEntity().getContent();
                logger.info("client fail " + result);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.getMessage(), ex);
        } finally {
            httppost.releaseConnection();
        }
        return result;

    }

}
